import tushare as ts

#import utils

code_list = ["000536", "000529", "000526", "000524", "000523", "000517",
             "002263", "002227", "002096", "002071", "002069", "002068", "002067", "002066", "002061", "002057", "002056", "002055",
             "601607", "002682", "002575", "002484", "002483", "002480", "002478", "002474", "002472", "002471",
             "600011", "300202", "300190", "300187", "300185", "300183", "300179", "300175", "300174", "300173",
             "600611", "600513", "600278", "600276", "600275", "600273", "600269", "600266"]
num_codes = len(code_list)
begin = "2004-01-01"
end = "2018-12-36"
for i, code in enumerate(code_list):
    print("processing code: ", code)
    data = ts.get_h_data(code, autype=None, start=begin, end=end)
    print()
    if data.empty:
        #utils.error("fail to fetch code:", code)
        continue
    data.sort_index(inplace=True)
    fq = ts.get_h_data(code, autype="hfq", start=begin, end=end)
    print()
    if fq.empty:
        fq = ts.get_h_data(code, autype="qfq", start=begin, end=end)
        print()
        if fq.empty:
            #utils.error("missing fuquan data of code:", code)
            continue
    fq.sort_index(inplace=True)
    data["adj_close"] = fq["close"]
    data.to_csv("data/" + code + ".asc")
    print("progress %.2f%%" % (i * 100.0 / num_codes))